<?php

/*
    Dill is a tool for creating websites with PHP.
    Copyright (C) 2014  Jannik Haberbosch

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/


require_once( "WebsiteProjectElement.php" );

abstract class WebsiteProjectFileElement extends WebsiteProjectElement
{
	static protected $directory;
	static protected $abspath_directory;
	protected $abspath_file;
	
	
	function __construct( $name )
	{
		// Attempts to create the required file if it doesn't yet exist.
		if( !file_exists( makepath( static::$abspath_directory, $name ) ) )
		{
			$result = file_put_contents(
				makepath(
					static::$abspath_directory,
					$name
				),
				""
			);
			if( $result === FALSE )
			{
				throw new Exception( "Error creating a file." );
			}
		}
		
		// In any case, creates an object representing the file.
		parent::__construct( $name );

		$this->abspath_file = makepath( static::$abspath_directory, $name );
	}

	
	public function get_content()
	{
		/* On success, returns the contents (string) of a file.  On failure,
		returns FALSE.
		
		Parameters		Void
		Return values	String | boolean (FALSE)

		*/
		return file_get_contents( $this->abspath_file );
	}

	
	public function set_content( $data )
	{
		/* On success, returns the number of bytes written to the file.  On
		failure, returns FALSE.

		Parameters		$data
						mixed
						The data to write to the file.
						
		Return values	integer | boolean (FALSE)

		*/
		return file_put_contents( $this->abspath_file, $data );
	}



	public function update_file_related_properties( $new_name )
	{
		$this->name = $new_name;
		$this->abspath_file = makepath( static::$abspath_directory, $new_name );
	}

	
	static public function set_directory( $directory )
	{
		static::$directory = $directory;
	}

	
	static public function get_directory()
	{
		return static::$directory;
	}


	static public function set_abspath_directory( $abspath_website_project )
	{
		static::$abspath_directory = makepath(
			$abspath_website_project,
			static::$directory
		);
	}


	static public function get_abspath_directory()
	{
		return static::$abspath_directory;
	}


	public function get_abspath_file()
	{
		return $this->abspath_file;
	}
}

?>
